//
//  YWMonitorAlarmTableViewCell.m
//  maintain
//
//  Created by 烟雨云渊 on 17/1/9.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWMonitorAlarmTableViewCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "UILabel+AttributedText.h"
#import "YWMonitorAlarmModel.h"
#import "YWField.h"
#import "YWCommonTool.h"
#import "YWEquipment.h"
#import "YWBuildingManager.h"
#import "YWLocation.h"
#import "YYLabel.h"

@interface YWMonitorAlarmTableViewCell()
/**背景UIView*/
@property (nonatomic, strong) UIView * backGroundIV;
/**标题*/
@property (nonatomic, strong) YYLabel * titleLabel;
/**时间*/
@property (nonatomic, strong) YYLabel * timeLabel;
/**状态*/
@property (nonatomic, strong) YYLabel * statusLabel;
/**设备名称*/
@property (nonatomic, strong) YYLabel * equipmentNameLabel;
/**自编码*/
@property (nonatomic, strong) YYLabel * ownCodeLabel;
/**位置*/
@property (nonatomic, strong) YYLabel * locationLabel;
/**异常参数*/
@property (nonatomic, strong) YYLabel * unnormalLabel;
/**参数名*/
@property (nonatomic, strong) YYLabel * parameterLabel;
/**正常值*/
@property (nonatomic, strong) YYLabel * normalLabel;
@end

@implementation YWMonitorAlarmTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        self.backgroundColor = [UIColor customBackgroudColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpSubviews];
    }
    return self;
}

- (void)setMonitorAlarm:(YWMonitorAlarmModel *)monitorAlarm{
    _monitorAlarm = monitorAlarm;
    self.titleLabel.text = _monitorAlarm.title;
    self.statusLabel.text =_monitorAlarm.content;
    self.timeLabel.text = [YWCommonTool showTime:_monitorAlarm.createTime/1000 andFormatter:KTimeFormatMonthDay];
    NSString *parameterStr = [NSString stringWithFormat:@"%@:   ",_monitorAlarm.field.fieldName];
    
//    self.parameterLabel.text = parameterStr;

    NSString*equipmentName =_monitorAlarm.equipment.equipmentName.length?_monitorAlarm.equipment.equipmentName:@"无";
    _equipmentNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:[NSString stringWithFormat:@"%@：   ",NSLocalizedString(@"equipment", nil)] firstColor:[UIColor customTextGrayBlackColor] secondText:equipmentName secondColor:[UIColor blackColor] font:kContentFont];
    NSString*ownCode = _monitorAlarm.equipment.ownCode.length?_monitorAlarm.equipment.ownCode:@"无";
    _ownCodeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:[NSString stringWithFormat:@"%@：",NSLocalizedString(@"ownCode", nil)] firstColor:[UIColor customTextGrayBlackColor] secondText:ownCode secondColor:[UIColor blackColor] font:kContentFont];
    
    NSString * locationStr = [[YWBuildingManager shareManager] areaLocationWithLocationId:_monitorAlarm.equipment.locationId];
    _locationLabel.attributedText = [YWCommonTool attributedTextWithFirstText:[NSString stringWithFormat:@"%@：   ",NSLocalizedString(@"location", nil)] firstColor:[UIColor customTextGrayBlackColor] secondText:locationStr.length?locationStr:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    self.unnormalLabel.text = NSLocalizedString(@"exceptionParameter", nil);
    NSString *strValue = @(_monitorAlarm.warnValue).stringValue;
    NSString *value = [NSString stringWithFormat:@"%@",@(strValue.doubleValue)];
    if (_monitorAlarm.warnValue<_monitorAlarm.field.minFieldValue || _monitorAlarm.warnValue>_monitorAlarm.field.maxFieldValue) {
        _parameterLabel.attributedText = [YWCommonTool attributedTextWithFirstText:parameterStr firstColor:[UIColor customTextGrayBlackColor] secondText:[NSString stringWithFormat:@"%@",value] secondColor:[UIColor redColor] font:kContentFont];
    }else{
        _parameterLabel.attributedText = [YWCommonTool attributedTextWithFirstText:parameterStr firstColor:[UIColor customTextGrayBlackColor] secondText:[NSString stringWithFormat:@"%@",value] secondColor:[UIColor customTextGrayColor] font:kContentFont];
    }
    
    NSString * normalValue = [NSString stringWithFormat:@"正常值   %ld-%ld",(long)_monitorAlarm.field.minFieldValue,(long)_monitorAlarm.field.maxFieldValue];
    self.normalLabel.text = normalValue;
    
    self.statusLabel.preferredMaxLayoutWidth = self.statusLabel.frame.size.width;
    self.parameterLabel.preferredMaxLayoutWidth = self.parameterLabel.frame.size.width;
    self.locationLabel.preferredMaxLayoutWidth = self.locationLabel.frame.size.width;
    self.normalLabel.preferredMaxLayoutWidth = self.normalLabel.frame.size.width;
}

- (void)setUpSubviews{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_backGroundIV.mas_top).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_timeLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.equipmentNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_statusLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.ownCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_equipmentNameLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_ownCodeLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.unnormalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_locationLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];
    [self.parameterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_unnormalLabel.mas_bottom).offset(8);
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8);
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8);
    }];

    [self.normalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_parameterLabel.mas_bottom).offset(8).priorityHigh();
        make.left.mas_equalTo(_backGroundIV.mas_left).offset(8).priorityHigh();
        make.right.mas_equalTo(_backGroundIV.mas_right).offset(-8).priorityHigh();
        make.bottom.mas_equalTo(_backGroundIV.mas_bottom).offset(-8).priorityHigh();
    }];
    
    [self.backGroundIV mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(kMarginW).priorityHigh();
        make.left.mas_equalTo(self.contentView.mas_left).offset(kMarginW).priorityHigh();
        make.right.mas_equalTo(self.contentView.mas_right).offset(-kMarginW).priorityHigh();
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(-kMarginW).priorityHigh();
    }];
}

- (UIView *)backGroundIV{
    if (!_backGroundIV) {
        UIView *backGroundIV = [[UIView alloc] init];
        backGroundIV.clipsToBounds = YES;
        backGroundIV.layer.cornerRadius = 4.0;
        backGroundIV.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:backGroundIV];
        _backGroundIV = backGroundIV;
    }
    return _backGroundIV;
}

- (YYLabel *)titleLabel{
    if (!_titleLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kBoldTitleFont;
        label.textColor = [UIColor blackColor];
        [self.backGroundIV addSubview:label];
        _titleLabel = label;
    }
    return _titleLabel;
}

- (YYLabel *)timeLabel{
    if (!_timeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        [self.backGroundIV addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}
- (YYLabel *)statusLabel{
    if (!_statusLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        [self.backGroundIV addSubview:label];
        _statusLabel = label;
    }
    return _statusLabel;
}

- (YYLabel *)equipmentNameLabel{
    if (!_equipmentNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        [self.backGroundIV addSubview:label];
        _equipmentNameLabel = label;
    }
    return _equipmentNameLabel;
}

- (YYLabel *)ownCodeLabel{
    if (!_ownCodeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        [self.backGroundIV addSubview:label];
        _ownCodeLabel = label;
    }
    return _ownCodeLabel;
}

- (YYLabel *)locationLabel{
    if (!_locationLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        [self.backGroundIV addSubview:label];
        _locationLabel = label;
    }
    return _locationLabel;
}

- (YYLabel *)unnormalLabel{
    if (!_unnormalLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        [self.backGroundIV addSubview:label];
        _unnormalLabel = label;
    }
    return _unnormalLabel;
}

- (YYLabel *)parameterLabel{
    if (!_parameterLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        [self.backGroundIV addSubview:label];
        _parameterLabel = label;
    }
    return _parameterLabel;
}

- (YYLabel *)normalLabel{
    if (!_normalLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayBlackColor];
        [self.backGroundIV addSubview:label];
        _normalLabel = label;
    }
    return _normalLabel;
}

@end
